//
//  ProductDetailsViewController.m
//  CommunitySupermarketProiect
//
//  Created by 吕惠卿 on 2017/10/28.
//  Copyright © 2017年 吕惠卿. All rights reserved.
//

#import "ProductDetailsViewController.h"
#import "MainTouchTableTableView.h"
#import "MYSegmentView.h"
@interface ProductDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) BOOL canScroll;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
@property (nonatomic, strong) MainTouchTableTableView *tableView;
@property (nonatomic, strong) MYSegmentView *segmentView;
@property (nonatomic, strong) UIImageView *headImageView;

@end
static CGFloat const headViewHeight = 500.0f;

@implementation ProductDetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    WhiteColor;
    [self creatNavigationItem];
    [self.navigationController.navigationBar setAlpha:0];
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:[self tableView]];
    
    
    [self.tableView addSubview:[self headImageView]];
    
    //    监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leaveTop:) name:@"leaveTop" object:nil];
}
- (void)leaveTop:(NSNotification *)notification{
    
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    /**
     * 处理联动
     */
    //获取滚动视图y值的偏移量
    
    CGFloat yOffset  = scrollView.contentOffset.y;
    
    self.navigationController.navigationBar.alpha = (headViewHeight+yOffset)/(headViewHeight-64);
    
    CGFloat tabOffsetY = [_tableView rectForSection:0].origin.y-64.0f;
    
    CGFloat offsetY = scrollView.contentOffset.y;
    
    _isTopIsCanNotMoveTabViewPre = _isTopIsCanNotMoveTabView;
    
    if (offsetY >= tabOffsetY) {
        
        //不能滑动
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        _isTopIsCanNotMoveTabView = YES;
        
    }else{
        
        //可以滑动
        _isTopIsCanNotMoveTabView = NO;
        
    }
    if (_isTopIsCanNotMoveTabView != _isTopIsCanNotMoveTabViewPre) {
        
        if (!_isTopIsCanNotMoveTabViewPre && _isTopIsCanNotMoveTabView) {
            //NSLog(@"子视图控制器滑动到顶端");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"goTop" object:nil userInfo:@{@"canScroll":@"1"}];
            _canScroll = NO;
        }
        if(_isTopIsCanNotMoveTabViewPre && !_isTopIsCanNotMoveTabView){
            //NSLog(@"父视图控制器滑动到顶端");
            if (!_canScroll) {
                
                scrollView.contentOffset = CGPointMake(0, tabOffsetY);
            }
        }
    }
    
    /**
     * 处理头部视图
     */
    if(yOffset < -headViewHeight) {
        CGRect f = self.headImageView.frame;
        f.origin.x = (yOffset*WIDTH/headViewHeight+WIDTH)/2;
        f.origin.y= yOffset ;
        f.size.height = -yOffset;
        f.size.width = -yOffset*WIDTH/headViewHeight;
        //改变头部视图的fram
        self.headImageView.frame= f;
    }
}

- (UIImageView *)headImageView
{
    if (_headImageView == nil)
    {
        _headImageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, -headViewHeight , WIDTH,headViewHeight)];
        [_headImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Animal" ofType:@"jpeg"]]];
        _headImageView.backgroundColor = [UIColor whiteColor];
        _headImageView.userInteractionEnabled = YES;
    }
    return _headImageView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[MainTouchTableTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentInset = UIEdgeInsetsMake(headViewHeight, 0, 0, 0);
        _tableView.backgroundColor = [UIColor redColor];
        _tableView.rowHeight = HEIGHT;
    }
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.contentView addSubview:self.segmentView];
        return cell;
    }
    else{
        DescriptionTableViewCell *cell = [[DescriptionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor greenColor];
        return cell;
    }
 
}
//分段控制器
- (UIView *)segmentView
{
    if(!_segmentView){
        DetailsTreasureViewController *firstViewController = [[DetailsTreasureViewController alloc] init];
        firstViewController.title = @"宝贝详情";
        
        EvaluateTreasureViewController *secondViewController = [[EvaluateTreasureViewController alloc] init];
        secondViewController.title = @"宝贝评价";
        
        NSArray *controllerArray = @[firstViewController, secondViewController];
        NSArray *titleArray = @[@"宝贝详情", @"宝贝评价"];
        CGFloat lineWidth = [@"second" boundingRectWithSize:CGSizeMake(100, 21) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size.width;
        
        MYSegmentView *tempView = [[MYSegmentView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) controllers:controllerArray titleArray:titleArray ParentController:self lineWidth:lineWidth lineHeight:3.0];
        _segmentView = tempView;
    }
    return _segmentView;
}

-(void)creatNavigationItem{
    UIButton *btn = [UIButton new];
    //    btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
- (void)btnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
